#include "widget.h"
#include "ui_widget.h"
#include"mydialog.h"
#include "myrole.h"
#include <QPixmap>
#include<QDebug>
#include<QGraphicsPixmapItem>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    setWindowTitle("SuperMarioRun!!!");
    setWindowIcon(QIcon(":/icons/resources/imgs/icons/marioicon.png"));
    this->Init();
    this->gameLoad();
}
Widget::~Widget()
{
    delete ui;
}
//整体初始化
void Widget::Init()
{
    //开始视图设置
    startViewInit();
    //按钮设置
    buttonInit();
    //背景音乐设置
    bgMusicInit();
    //游戏界面设置
    gameViewInit();
    //马里奥设置
    marioInit();
    //怪物设置
    monsterInit();
    //失败视图设置
    failViewInit();
    //通关视图设置
    successViewInit();
    //通关检测器设置
    gameWin();
    //掉坑检测设置
    dropDetect();
    //游戏简介设置
    introduceDalInit();
    //不能移动物体设置
    uMObjectInit();
    //烟囱喷火设置
    fireInit();
    //库巴设置
    kubaInit();
    //食人花
    shirenhuaInit();
    //章鱼初始化
    zhangyuInit();
    //小鸟初始化
    birdInit();
}
//进入游戏界面
void Widget::Init_Game()
{
    bgBgmPlayer->play();
    connect(bgBgmPlayer,&QMediaPlayer::mediaStatusChanged,bgBgmPlayer,&QMediaPlayer::play);
    this->setFixedSize(1050,336);
    //游戏界面加载
    mGameView.setSceneRect(QRect(0,0,700,224));
    mGameView.show();
    //怪物加载
    monsterIcon.setZValue(2);
    monsterIcon.moveBy(200-monsterIcon.pos().x(),184-monsterIcon.pos().y());
    monsterl=200,monsterr=260,monsterDir=1;
    monsterMoveTimer->start(10);
    monsterCrashTimer->start(10);
    //马里奥加载
    marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(176,0,16,32)));
    monsterIcon.setZValue(3);
    y0=168;
    tmpMoveDir1=0;
    tmpMovwDir2=0;
    x0=118,x1=3392-(700-118);
    tmpdir=0;
    timecnt=0;
    marioIcon.moveBy(118-marioIcon.pos().x(),168-marioIcon.pos().y());
    marioMoveTimer->start(10);
    //通关检测
    successDetectTimer->start(10);
    //掉坑检测
    caveDropDetectTimer->start(10);
    //烟囱喷火
     fireUpMoveTimer->start(10);
    for(int i=0;i<6;i++){
         fireUpList[i]->show();
    }
    //库巴初始化
    kubaMoveTimer->start(10);
    kubaActionTimer->start(100);
    kubaCrashTimer->start(10);
    kubaGuangboMvTimer->start(10);
    //食人花初始化
    shirenhuaTimer->start(100);
    //章鱼初始化
    zhangyuMoveTimer->start(200);
    zyActionTimer->start(100);
    //小鸟初始化
    birdMoveTimer->start(10);
    bdActionTimer->start(100);
    boomMoveTimer->start(10);
}
//马里奥初始化
void Widget::marioInit()
{
    //马里奥形象设置
    mGameScene.addItem(&marioIcon);
    //人物移动
    marioMoveTimer=new QTimer(this);
    connect(marioMoveTimer,&QTimer::timeout,[this](){
        //检测左右移动是否碰上物体
        bool zuof=1;
        bool youf=1;
        qreal my0=marioIcon.boundingRectF().y();
        qreal mx0=marioIcon.boundingRectF().x();
        qreal my1=marioIcon.boundingRectF().y()+marioIcon.boundingRectF().height();
        qreal mx1=marioIcon.boundingRectF().x()+marioIcon.boundingRectF().width();
        for(int i=0;i<objectList.size();i++){
            qreal oy0=objectList[i].y();
            qreal ox0=objectList[i].x();
            qreal oy1=objectList[i].y()+objectList[i].height();
            qreal ox1=objectList[i].x()+objectList[i].width();
            if(my0<oy1&&my1>oy0&&(mx0<=ox1&&mx1>=ox0)){
                if(mx0==ox1)  zuof=0;
                if(mx1==ox0)  youf=0;
                if(marioIcon.boundingRectF().intersects(objectList[i])) zuof=0,youf=0;
                break;
            }
        }
        //界面移动和马里奥的左右移动
        if(tmpMoveDir1==Qt::Key_A){
            if(zuof) marioIcon.moveBy(-1,0);
            if(marioIcon.pos().x()<=x0){
                mGameView.setSceneRect(QRect(0,0,700,224));
            }else {
                mGameView.setSceneRect(QRect(fmin(marioIcon.pos().x()-x0,3392-700),0,700,224));
            }
        }else if(tmpMoveDir1==Qt::Key_D){
            if(youf) marioIcon.moveBy(1,0);
            if(marioIcon.pos().x()>=x1){
                mGameView.setSceneRect(QRect(3392-700,0,700,224));
            }else {
                mGameView.setSceneRect(QRect(fmax(marioIcon.pos().x()-x0,0),0,700,224));
            }
        }
        //检测下坠是否碰上物体
        for(int i=0;i<objectList.size();i++){
            qreal oy0=objectList[i].y();
            qreal ox0=objectList[i].x();
            qreal oy1=objectList[i].y()+objectList[i].height();
            qreal ox1=objectList[i].x()+objectList[i].width();
            if(mx0<ox1&&mx1>ox0&&my1>=oy0&&v<=0&&my1<oy1) {
                v=0,marioIcon.moveBy(0,oy0-32-marioIcon.pos().y());
                break;
            }
        }
        //插入对应动画
        if(v==0){
            if(tmpMoveDir1==0){
                if(tmpdir==Qt::Key_D) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(176,0,16,32)));
                else if(tmpdir==Qt::Key_A) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(176,0,16,32)));
                timecnt=0;
            }else {
                timecnt=timecnt%120;
                if(tmpMoveDir1==Qt::Key_A){
                    if(timecnt==0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(80,0,16,32)));
                    else if(timecnt==40) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(96,0,16,32)));
                    else if(timecnt==80) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(112,0,16,32)));
                }else {
                    if(timecnt==0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(80,0,16,32)));
                    else if(timecnt==40) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(96,0,16,32)));
                    else if(timecnt==80) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(112,0,16,32)));
                }
                timecnt+=10;
            }
        }else {
            if(tmpMoveDir1==0){
                if(tmpdir==Qt::Key_D) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(176,0,16,32)));
                else if(tmpdir==Qt::Key_A) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(176,0,16,32)));
            }else {
                if(tmpMoveDir1==Qt::Key_A){
                    if(v>0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(304,0,16,32)));
                    else if(v<=0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros1.png").copy(QRect(193,0,16,32)));
                }else {
                    if(v>0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(304,0,16,32)));
                    else if(v<=0) marioIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/mario_bros.png").copy(QRect(193,0,16,32)));
                }
            }
        }
        tmpdir=tmpMoveDir1;
        //马里奥的下坠运动
        marioIcon.moveBy(0,-v);
        v-=g;

    });
}
//按钮设置
void Widget::buttonInit()
{
    //按钮音效
    buttonAudioPlayer = new QMediaPlayer(this);
    buttonAudioOutput = new QAudioOutput(this);
    buttonAudioPlayer->setAudioOutput(buttonAudioOutput);
    buttonAudioPlayer->setSource(QUrl("qrc:/sfx/resources/sounds/sfx/clickbuttonsound.ogg"));
    buttonAudioOutput->setVolume(1.00);

    //开始按钮
    startBtn = new MButton(this);
    startBtn->setAutoRaise(true);
    startBtn->setFixedSize(182,52);
    startBtn->setIcon(QIcon(":/icons/resources/imgs/icons/startbtn.png"));
    startBtn->setIconSize(QSize(182,52));
    startBtn->move(60,70);
    startBtn->hide();

    connect(startBtn,&QToolButton::clicked,[this](){
        buttonAudioPlayer->play();
        Init_Game();
        startBtn->hide();
        introduceBtn->hide();
        escBtn->hide();
    });


    //退出按钮
    escBtn = new MButton(this);
    escBtn->setAutoRaise(true);
    escBtn->setFixedSize(182,52);
    escBtn->setIcon(QIcon(":/icons/resources/imgs/icons/escbtn.png"));
    escBtn->setIconSize(QSize(182,52));
    escBtn->move(464,70);
    escBtn->hide();

    connect(escBtn,&QToolButton::clicked,[this](){
        buttonAudioPlayer->play();
        close();
    });


    //再玩一次按钮
    onceMoreBtn = new MButton(this);
    onceMoreBtn->setAutoRaise(true);
    onceMoreBtn->setFixedSize(182,52);
    onceMoreBtn->setIcon(QIcon(":/icons/resources/imgs/icons/onceMoreBtn.png"));
    onceMoreBtn->setIconSize(QSize(182,52));
    onceMoreBtn->move(220,400);
    onceMoreBtn->hide();

    connect(onceMoreBtn,&QToolButton::pressed,[this](){
        buttonAudioPlayer->play();
        mFDalView.hide();
        backBtn->hide();
        onceMoreBtn->hide();
        failLabel->hide();
        restartGame();
    });

    //游戏介绍按钮
    introduceBtn = new MButton(this);
    introduceBtn->setAutoRaise(true);
    introduceBtn->setFixedSize(182,52);
    introduceBtn->setIcon(QIcon(":/icons/resources/imgs/icons/introducebtn.png"));
    introduceBtn->setIconSize(QSize(182,52));
    introduceBtn->move(262,70);
    introduceBtn->hide();

    connect(introduceBtn,&QToolButton::clicked,[this](){
        buttonAudioPlayer->play();
        introduceDal->exec();
        label->show();
        word->show();
    });

    //返回主页按钮
    backBtn= new MButton(this);
    backBtn->setAutoRaise(true);
    backBtn->setFixedSize(182,52);
    backBtn->setIcon(QIcon(":/icons/resources/imgs/icons/backBtn.png"));
    backBtn->setIconSize(QSize(182,52));
    backBtn->move(220,500);
    backBtn->hide();
    connect(backBtn,&QToolButton::pressed,[this](){
        buttonAudioPlayer->play();
        mFDalView.hide();
        mSucView.hide();
        backBtn->hide();
        onceMoreBtn->hide();
        failLabel->hide();
        startWidget();
    });
}
//失败视图设置
void Widget::failViewInit()
{
    //失败字体设置
    failLabel=new QLabel(this);
    failLabel->setText("失败了");
    QFont a=QFont();
    a.setFamily("幼圆");
    a.setPointSize(80);
    a.setBold(true);
    failLabel->setFont(a);
    failLabel->move(150,130);
    QPalette palette = failLabel->palette();
    palette.setColor(QPalette::WindowText, QColor(247, 220, 111));
    failLabel->setPalette(palette);
    failLabel->hide();
    //场景视图设置
    mFDalScene.addPixmap(QPixmap(":/backgrounds/resources/imgs/backgrounds/faildialog.png"));
    mFDalView.setScene(&mFDalScene);
    mFDalView.setSceneRect(QRect(0,0,614,612));
    mFDalView.resize(614,612);
    mFDalView.setParent(this);
    mFDalView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mFDalView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mFDalView.hide();
}
//失败视图展示
void Widget::failWidget()
{
    turnOffTimer();
    failBgmPlayer->play();
    this->setFixedSize(614,612);
    mFDalView.show();
    backBtn->setDown(false);
    failLabel->show();
    failLabel->raise();
    onceMoreBtn->setDown(false);
    backBtn->show();
    backBtn->raise();
    onceMoreBtn->raise();
    onceMoreBtn->show();

}
//开始视图设置
void Widget::startViewInit()
{
    mStartView.setParent(this);
    mStartView.resize(700,398);
    mStartView.setSceneRect(QRect(0,0,700,398));
    mStartView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mStartView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mStartScene.addPixmap(QPixmap(":/backgrounds/resources/imgs/backgrounds/startbackground.jpg"));
    mStartView.setScene(&mStartScene);
}
//开始界面展示
void Widget::startWidget()
{
    this->setFixedSize(700,398);
    mStartView.show();
    startBtn->raise();
    introduceBtn->raise();
    escBtn->raise();
    startBtn->show();
    escBtn->show();
    introduceBtn->show();
}
//通关界面展示
void Widget::successWidget()
{
    sucBgmPlayer->play();
    turnOffTimer();
    this->setFixedSize(620,620);
    mSucView.show();
    backBtn->show();
    backBtn->setDown(false);
    backBtn->raise();
}
//通关界面设置
void Widget::successViewInit()
{
    mSucScene.addPixmap(QPixmap(":/backgrounds/resources/imgs/backgrounds/successpicture.png"));
    mSucView.setScene(&mSucScene);
    mSucView.setSceneRect(QRect(0,0,620,620));
    mSucView.resize(620,620);
    mSucView.setParent(this);
    mSucView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mSucView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mSucView.hide();
}
//游戏介绍对话框
void Widget::introduceDalInit()
{
    introduceDal=new MDialog(this);
    introduceDal->setFixedSize(612,616);
    label=new QLabel(introduceDal);
    label->setPixmap(QPixmap(":/backgrounds/resources/imgs/backgrounds/introducebackground.png"));
    label->resize(label->pixmap().width(),label->pixmap().height());
    word= new QLabel(introduceDal);
    word->setFixedSize(421,190);
    word->move(88,232);
    word->setText("W键向上跳,A键向左移动，D键向右移动，进入城堡即可获得胜利，注意要躲避怪物和怪物的伤害哦");
    QFont font=QFont();
    font.setFamily("宋体");
    font.setPointSize(20);
    font.setBold(true);
    word->setFont(font);
    QPalette palette = word->palette();
    palette.setColor(QPalette::WindowText, QColor(23, 32, 42));
    word->setPalette(palette);
    word->setWordWrap(&free);
    word->setAlignment(Qt::AlignCenter) ;

}
//背景音乐设置
void Widget::bgMusicInit()
{
    //设置背景音乐
    bgBgmPlayer = new QMediaPlayer(this);
    bgBgmoutput = new QAudioOutput(this);
    bgBgmPlayer->setAudioOutput(bgBgmoutput);
    bgBgmPlayer->setSource(QUrl("qrc:/bgm/resources/sounds/bgm/mariobackgroundmusic.mp3"));
    bgBgmoutput->setVolume(0.80);

    sucBgmPlayer=new QMediaPlayer(this);
    sucBgmoutput=new QAudioOutput(this);
    failBgmoutput=new QAudioOutput(this);
    failBgmPlayer=new QMediaPlayer(this);
    sucBgmPlayer->setAudioOutput(sucBgmoutput);
    failBgmPlayer->setAudioOutput(failBgmoutput);
    sucBgmPlayer->setSource(QUrl("qrc:/bgm/resources/sounds/bgm/马里奥获胜音效.mp3"));
    failBgmPlayer->setSource(QUrl("qrc:/bgm/resources/sounds/bgm/马里奥阵亡音效.mp3"));
    sucBgmoutput->setVolume(1.00);
    failBgmoutput->setVolume(1.00);

}
//怪物初始化
void Widget::monsterInit()
{

    //怪物
    monsterIcon.setPixmap(QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(QRect(0,16,16,16)));
    mGameScene.addItem(&monsterIcon);
    monsterMoveTimer =new QTimer(this);
    monsterList.append(&monsterIcon);
    connect(monsterMoveTimer,&QTimer::timeout,[this](){
        if(monsterDir) monsterIcon.moveBy(0.5,0);
        else if(!monsterDir) monsterIcon.moveBy(-0.5,0);
        if(monsterIcon.pos().x()>=monsterr) monsterDir=0;
        if(monsterIcon.pos().x()<=monsterl) monsterDir=1;
    });

    //怪物碰撞检测
    monsterCrashTimer=new QTimer(this);
    connect(monsterCrashTimer,&QTimer::timeout,[this](){
        for(int i=0;i<monsterList.size();i++){
            if(monsterList[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });
}
//游戏界面设置
void Widget::gameViewInit()
{
    //初始化开始界面
    mGameView.setParent(this);
    mGameView.resize(1050,336);
    mGameView.scale(1.5,1.5);
    mGameView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mGameView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    mGameView.setSceneRect(QRect(0,0,700,224));
    mGameScene.addPixmap(QPixmap(":/backgrounds/resources/imgs/backgrounds/gamepicture.png"));
    mGameView.setScene(&mGameScene);
    mGameView.hide();
}
//整个游戏界面加载
void Widget::gameLoad()
{
    this->setFixedSize(700,398);
    mStartView.show();
    startBtn->show();
    escBtn->show();
    introduceBtn->show();

}
//重新复活
void Widget::restartGame()
{
    this->setFixedSize(1050,336);
    mGameView.show();
    turnOnTimer();
    marioIcon.moveBy(0,0-marioIcon.pos().y());
}
//掉坑检测
void Widget::dropDetect()
{
    caveDropDetectTimer=new QTimer(this);
    connect(caveDropDetectTimer,&QTimer::timeout,[this](){
        if(marioIcon.pos().y()>224) {
            mGameView.hide();
            failWidget();
        }
    });
}
//不能移动物体设置
void Widget::uMObjectInit()
{
    objectList.append(QRectF(0,200,1103,23));
    objectList.append(QRectF(1138,200,238,23));
    objectList.append(QRectF(1426,200,1020,23));
    objectList.append(QRectF(2481,200,3392-2481,23));
    objectList.append(QRectF(-100,0,100,224));
    objectList.append(QRectF(3392,0,100,224));
    objectList.append(QRectF(448,168,32,32));
    objectList.append(QRectF(608,152,33,48));
    objectList.append(QRectF(736,136,32,63));
    objectList.append(QRectF(912,136,31,62));
    objectList.append(QRectF(2144,184,64,16));
    objectList.append(QRectF(2160,168,48,16));
    objectList.append(QRectF(2176,152,32,16));
    objectList.append(QRectF(2192,136,16,16));
    objectList.append(QRectF(2239,184,64,16));
    objectList.append(QRectF(2239,168,48,16));
    objectList.append(QRectF(2239,152,32,16));
    objectList.append(QRectF(2239,136,16,16));
    objectList.append(QRectF(2368,184,80,16));
    objectList.append(QRectF(2384,168,64,16));
    objectList.append(QRectF(2400,152,48,16));
    objectList.append(QRectF(2416,136,32,16));
    objectList.append(QRectF(2608,168,32,32));
    objectList.append(QRectF(2864,168,32,32));
    objectList.append(QRectF(2895,184,144,16));
    for(int i=1;i<=4;i++){
        objectList.append(QRectF(2479,184-(i-1)*16,64-(i-1)*16,16));
    }
    for(int i=1;i<=8;i++){
        objectList.append(QRectF(2895+(i-1)*16,184-(i-1)*16,144-(i-1)*16,16));
    }
    objectList.append(QRectF(3168,184,16,16));




}
//烟囱喷火设置
void Widget::fireInit()
{
    //112,176,16,16
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),152,456));
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),136,616));
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),120,744));
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),120,920));
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),152,2616));
    fireUpList.append(new guangBo(QPixmap(":/icons/resources/imgs/icons/item_objects.png").copy(112,176,16,16),152,2872));
    for(int i=1;i<=6;i++){
        mGameScene.addItem(fireUpList[i-1]);
        fireUpList[i-1]->hide();
    }
    fireUpList[0]->moveBy(456,152);
    fireUpList[1]->moveBy(616,136);
    fireUpList[2]->moveBy(744,120);
    fireUpList[3]->moveBy(920,120);
    fireUpList[4]->moveBy(2616,152);
    fireUpList[5]->moveBy(2872,152);
    fireUpMoveTimer=new QTimer(this);
    connect(fireUpMoveTimer,&QTimer::timeout,[this](){
        for(int i=0;i<fireUpList.size();i++){
            if(fireUpList[i]->pos().y()+16.0>=-20.0) {
                fireUpList[i]->moveBy(0,-0.5);
            }else {
                fireUpList[i]->moveBy(0,fireUpList[i]->sy-fireUpList[i]->pos().y());
            }
            if(fireUpList[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });


}
//通关检测器
void Widget::gameWin()
{
    successDoor=new QRectF(3262,172,17,27);
    successDetectTimer=new QTimer(this);
    connect(successDetectTimer,&QTimer::timeout,[this](){
        if(successDoor->intersects(marioIcon.boundingRectF())){
            mGameView.hide();

            successWidget();
        }
    });
}
//打开游戏定时器和背景音乐
void Widget::turnOnTimer()
{
    kubaMoveTimer->start(10);
    monsterCrashTimer->start(10);
    marioMoveTimer->start(10);
    monsterMoveTimer->start(10);
    fireUpMoveTimer->start(10);
    caveDropDetectTimer->start(10);
    successDetectTimer->start(10);
    kubaActionTimer->start(100);
    kubaCrashTimer->start(10);
    kubaGuangboMvTimer->start(10);
    shirenhuaTimer->start(100);
    zhangyuMoveTimer->start(200);
    zyActionTimer->start(100);
    birdMoveTimer->start(10);
    bdActionTimer->start(100);
    boomMoveTimer->start(10);
    bgBgmPlayer->play();
    connect(bgBgmPlayer,&QMediaPlayer::mediaStatusChanged,bgBgmPlayer,&QMediaPlayer::play);
}
//关闭游戏定时器和背景音乐
void Widget::turnOffTimer()
{
    kubaActionTimer->stop();
    kubaMoveTimer->stop();
    successDetectTimer->stop();
    monsterCrashTimer->stop();
    marioMoveTimer->stop();
    monsterMoveTimer->stop();
    fireUpMoveTimer->stop();
    caveDropDetectTimer->stop();
    kubaCrashTimer->stop();
    kubaGuangboMvTimer->stop();
    shirenhuaTimer->stop();
    zyActionTimer->stop();
    zhangyuMoveTimer->stop();
    birdMoveTimer->stop();
    bdActionTimer->stop();
    boomMoveTimer->stop();
    disconnect(bgBgmPlayer,&QMediaPlayer::mediaStatusChanged,bgBgmPlayer,&QMediaPlayer::play);
    bgBgmPlayer->stop();
}

void Widget::stopViewInit()
{

}
//库巴设置
void Widget::kubaInit()
{
    //752,0,32,32
    //库巴的设置
    QPixmap q1=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(656,0,32,32);
    QPixmap q2=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(688,0,32,32);
    QPixmap q3=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(720,0,32,32);
    QPixmap q4=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(752,0,32,32);
    kubaList.append(new kuba(481,575,481,168,q1,q2,q3,q4,1));
    kubaList.append(new kuba(774,909-32,774,168,q1,q2,q3,q4,1));
    kubaList.append(new kuba(2641,2860-32,2641,168,q1,q2,q3,q4,1));
    kubaMoveTimer=new QTimer(this);
    kubaActionTimer=new QTimer(this);
    for(int i=0;i<kubaList.size();i++){
        mGameScene.addItem(kubaList[i]);
        kubaList[i]->moveBy(kubaList[i]->sx,kubaList[i]->sy);
    }
    connect(kubaActionTimer,&QTimer::timeout,[this](){
        for(int i=0;i<kubaList.size();i++){
            if(!kubaList[i]->dir) kubaList[i]->setPixmap(kubaList[i]->pList[kubaList[i]->Id++]);
            if(kubaList[i]->dir) kubaList[i]->setPixmap(kubaList[i]->pList[kubaList[i]->Id++].transformed(QTransform().scale(-1, 1)));
            kubaList[i]->Id=(kubaList[i]->Id)%kubaList[i]->pList.size();
        }
    });
    connect(kubaMoveTimer,&QTimer::timeout,[this](){
        for(int i=0;i<kubaList.size();i++){
            if(kubaList[i]->pos().x()<=kubaList[i]->l) kubaList[i]->dir=1;
            if(kubaList[i]->pos().x()>=kubaList[i]->r) kubaList[i]->dir=0;
            if(kubaList[i]->dir) kubaList[i]->moveBy(0.2,0);
            else kubaList[i]->moveBy(-0.2,0);
        }
    });
    kubaCrashTimer=new QTimer(this);
    connect(kubaCrashTimer,&QTimer::timeout,[this](){
        for(int i=0;i<kubaList.size();i++){
            if(kubaList[i]->boundingRectF().intersects(marioIcon.boundingRectF())) {
                mGameView.hide();
                failWidget();
            }
        }
    });
    //库巴的火焰的设置
    //784,0,24,16
    QPixmap tmpq=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(784,0,24,16);
    for(int i=0;i<kubaList.size();i++){
        kubaGuangbo.append(new guangBo(QPixmap(),168,-1));

    }
    for(int i=0;i<kubaGuangbo.size();i++){
        mGameScene.addItem(kubaGuangbo[i]);
        kubaGuangbo[i]->moveBy(-100,168);
    }
    kubaGuangboMvTimer=new QTimer(this);
    connect(kubaGuangboMvTimer,&QTimer::timeout,[this, tmpq](){
        for(int i=0;i<kubaGuangbo.size();i++){
            if(kubaGuangbo[i]->pos().x()>=kubaList[i]->pos().x()-200&&kubaGuangbo[i]->pos().x()<=kubaList[i]->pos().x()+200){
                if(kubaGuangbo[i]->Dir) kubaGuangbo[i]->moveBy(0.6,0);
                else kubaGuangbo[i]->moveBy(-0.6,0);
            }else {
                if(!kubaList[i]->dir) kubaGuangbo[i]->moveBy(kubaList[i]->pos().x()-24-kubaGuangbo[i]->pos().x(),0),kubaGuangbo[i]->Dir=0;
                else kubaGuangbo[i]->moveBy(kubaList[i]->pos().x()+32-kubaGuangbo[i]->pos().x(),0),kubaGuangbo[i]->Dir=1;
            }
            if(!kubaGuangbo[i]->Dir) kubaGuangbo[i]->setPixmap(tmpq.transformed(QTransform().scale(-1,1))) ;
            else kubaGuangbo[i]->setPixmap(tmpq);
            if(kubaGuangbo[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });


}
//食人花设置
void Widget::shirenhuaInit()
{
    QPixmap p1=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(192,8,16,24);
    QPixmap p2=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(192+16,8,16,24);
    shirenhua.append(new UnmoveObject(1175,176,p1,p2));
    shirenhua.append(new UnmoveObject(1273,176,p1,p2));
    shirenhua.append(new UnmoveObject(2220,176,p1,p2));
    for(int i=0;i<shirenhua.size();i++){
        mGameScene.addItem(shirenhua[i]);
        shirenhua[i]->moveBy(shirenhua[i]->sx,shirenhua[i]->sy);
    }
    shirenhuaTimer=new QTimer(this);
    connect(shirenhuaTimer,&QTimer::timeout,[this](){
        for(int i=0;i<shirenhua.size();i++){
            shirenhua[i]->setPixmap(shirenhua[i]->actionP[shirenhua[i]->cnt]);
            shirenhua[i]->cnt=(shirenhua[i]->cnt+1)%shirenhua[i]->actionP.size();
            if(shirenhua[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });
}
//章鱼初始化
void Widget::zhangyuInit()
{
    QPixmap p1=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(592,8,16,24);
    QPixmap p2=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(608,8,16,24);
    zhangyuList.append(new zhangyu(80,170,1710-50,p1,p2));
    zhangyuList.append(new zhangyu(80,170,1780-50,p1,p2));
    zhangyuList.append(new zhangyu(80,170,1850-50,p1,p2));
    zhangyuList.append(new zhangyu(80,170,1500-50,p1,p2));
    zhangyuList.append(new zhangyu(80,170,1570-50,p1,p2));
    zhangyuList.append(new zhangyu(80,170,1640-50,p1,p2));
    for(int i=0;i<zhangyuList.size();i++){
        mGameScene.addItem(zhangyuList[i]);
        zhangyuList[i]->setPixmap(p1);
        zhangyuList[i]->moveBy(zhangyuList[i]->Sx,(qreal)(rand()%(int)(zhangyuList[i]->Floor-zhangyuList[i]->Ceil))+zhangyuList[i]->Ceil);
    }
    zhangyuMoveTimer=new QTimer(this);
    zyActionTimer=new QTimer(this);
    connect(zyActionTimer,&QTimer::timeout,[this](){
        for(int i=0;i<zhangyuList.size();i++){
            if(zhangyuList[i]->dir){
                zhangyuList[i]->setPixmap(zhangyuList[i]->zhangyuAction[zhangyuList[i]->cnt++]);
                zhangyuList[i]->cnt=zhangyuList[i]->cnt%zhangyuList[i]->zhangyuAction.size();
            }else {
                zhangyuList[i]->setPixmap(zhangyuList[i]->zhangyuAction[zhangyuList[i]->cnt++].transformed(QTransform().scale(1, -1)));
                zhangyuList[i]->cnt=zhangyuList[i]->cnt%zhangyuList[i]->zhangyuAction.size();
            }

        }
    });
    connect(zhangyuMoveTimer,&QTimer::timeout,[this](){
        for(int i=0;i<zhangyuList.size();i++){
            if(zhangyuList[i]->pos().y()<=zhangyuList[i]->Ceil) zhangyuList[i]->dir=0;
            if(zhangyuList[i]->pos().y()>=zhangyuList[i]->Floor) zhangyuList[i]->dir=1;
            if(zhangyuList[i]->dir) zhangyuList[i]->moveBy(0,-8);
            else zhangyuList[i]->moveBy(0,8);
            if(zhangyuList[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });

}
//小鸟初始化
void Widget::birdInit()
{
    QPixmap p1=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(624,16,16,16);
    QPixmap p2=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(640,16,16,16);
    birdList.append(new bird(1800+32,1982,40,p1,p2));
    birdList.append(new bird(1982+16,1982+16+150,60,p1,p2));
    birdList.append(new bird(1982+16+150+16,1982+16+150+16+150,0,p1,p2));
    birdList.append(new bird(1982+16+150+16+150+16,1982+16+150+16+150+16+150,20,p1,p2));
    for(int i=0;i<birdList.size();i++){
        mGameScene.addItem(birdList[i]);
        birdList[i]->setPixmap(p1);
        birdList[i]->moveBy((qreal)(rand()%(int)(birdList[i]->R-birdList[i]->L))+birdList[i]->L,birdList[i]->Sy);
    }
    birdMoveTimer=new QTimer(this);
    bdActionTimer=new QTimer(this);
    connect(bdActionTimer,&QTimer::timeout,[this](){
        for(int i=0;i<birdList.size();i++){
            if(!birdList[i]->dir){
                birdList[i]->setPixmap(birdList[i]->birdAction[birdList[i]->cnt++]);
                birdList[i]->cnt=birdList[i]->cnt%birdList[i]->birdAction.size();
            }else {
                birdList[i]->setPixmap(birdList[i]->birdAction[birdList[i]->cnt++].transformed(QTransform().scale(-1, 1)));
                birdList[i]->cnt=birdList[i]->cnt%birdList[i]->birdAction.size();
            }

        }
    });
    connect(birdMoveTimer,&QTimer::timeout,[this](){
        for(int i=0;i<birdList.size();i++){
            if(birdList[i]->pos().x()<=birdList[i]->L) birdList[i]->dir=1;
            if(birdList[i]->pos().x()>=birdList[i]->R) birdList[i]->dir=0;
            if(birdList[i]->dir) birdList[i]->moveBy(16.0/50,0);
            else birdList[i]->moveBy(-16.0/50,0);
            if(birdList[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });
    QPixmap pp=QPixmap(":/icons/resources/imgs/icons/enemies.png").copy(561,16,14,16);
    boomMoveTimer=new QTimer(this);
    birdBoomList.append(new guangBo(pp,0,0));//y x
    birdBoomList.append(new guangBo(pp,0,0));
    birdBoomList.append(new guangBo(pp,0,0));//y x
    birdBoomList.append(new guangBo(pp,0,0));
    for(int i=0;i<birdBoomList.size();i++){
        mGameScene.addItem(birdBoomList[i]);
        birdBoomList[i]->moveBy(-100,600);
    }
    connect(boomMoveTimer,&QTimer::timeout,[this](){
        for(int i=0;i<birdBoomList.size();i++){
            if(birdBoomList[i]->pos().y()>=184) {
                birdBoomList[i]->hide();
            }
            if(birdBoomList[i]->pos().y()>=260){
                birdBoomList[i]->show();
                birdBoomList[i]->moveBy(birdList[i]->pos().x()+1-birdBoomList[i]->pos().x(),birdList[i]->pos().y()+16-birdBoomList[i]->pos().y());
            }
            birdBoomList[i]->moveBy(0,1);
            if(birdBoomList[i]->boundingRectF().intersects(marioIcon.boundingRectF())){
                mGameView.hide();
                failWidget();
            }
        }
    });
}
void Widget::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_A:
            if(tmpMoveDir1==0) tmpMoveDir1=Qt::Key_A;
            else tmpMovwDir2=Qt::Key_A;
            break;
        case Qt::Key_D:
            if(tmpMoveDir1==0) tmpMoveDir1=Qt::Key_D;
            else tmpMovwDir2=Qt::Key_D;
            break;
        case Qt::Key_W:
            if(v==-0.12) v=4;
            break;
    }
}
void Widget::keyReleaseEvent(QKeyEvent *event)
{
    switch(event->key())
    {
        case Qt::Key_A:
            if(tmpMoveDir1==Qt::Key_A) tmpMoveDir1=tmpMovwDir2,tmpMovwDir2=0;
            else if(tmpMovwDir2==Qt::Key_A) tmpMovwDir2=0;
            break;
        case Qt::Key_D:
            if(tmpMoveDir1==Qt::Key_D) tmpMoveDir1=tmpMovwDir2,tmpMovwDir2=0;
            else if(tmpMovwDir2==Qt::Key_D) tmpMovwDir2=0;
            break;
    }
}
